package com.ruoyi.record.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.company.domain.XlkjCompany;
import com.ruoyi.company.service.IXlkjCompanyService;
import com.ruoyi.house.domain.XlkjHouse;
import com.ruoyi.house.service.IXlkjHouseService;
import com.ruoyi.incident.domain.XlkjIncident;
import com.ruoyi.incident.service.IXlkjIncidentService;
import com.ruoyi.place.domain.XlkjPlace;
import com.ruoyi.place.service.IXlkjPlaceService;
import com.ruoyi.population.domain.XlkjPopulation;
import com.ruoyi.population.service.IXlkjPopulationService;
import com.ruoyi.record.entity.RecordVo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 采集记录接口
 */
@RestController
@RequestMapping("/app/record/all")
public class RecordControllerAllApp extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IXlkjPopulationService populationService;
    @Autowired
    private IXlkjHouseService houseService;
    @Autowired
    private IXlkjCompanyService companyService;
    @Autowired
    private IXlkjPlaceService placeService;
    @Autowired
    private IXlkjIncidentService incidentService;

    @GetMapping("/list/{titIndex}/{timeIndex}/{address}")
    public TableDataInfo list(@PathVariable("titIndex") Long titIndex, @PathVariable("timeIndex") Long timeIndex, @PathVariable("address") String address){

        List<RecordVo> recordList = new ArrayList<>();
        if(address.equals("null")){
            address = "";
        }

        if( titIndex == 1){
            XlkjPopulation population = new XlkjPopulation();
            population.setAddress(address);
            startPage();
            List<XlkjPopulation> xlkjPopulations = populationService.selectXlkjPopulationList(population);
            for(XlkjPopulation population1:xlkjPopulations){
                RecordVo recordVo = new RecordVo();
                recordVo.setType("1");
                recordVo.setId(population1.getId());
                recordVo.setAddress(population1.getAddress());
                recordVo.setCreateTime(population1.getCreateTime());
                recordList.add(recordVo);
            }
        }

        if(titIndex == 2){
            XlkjHouse house = new XlkjHouse();
            house.setAddress(address);
            startPage();
            List<XlkjHouse> xlkjHouses = houseService.selectXlkjHouseList(house);
            for(XlkjHouse house1:xlkjHouses){
                RecordVo recordVo = new RecordVo();
                recordVo.setType("2");
                recordVo.setId(house1.getId());
                recordVo.setAddress(house1.getAddress());
                recordVo.setCreateTime(house1.getCreateTime());
                recordList.add(recordVo);
            }
        }
        if(titIndex == 3){
            XlkjCompany company = new XlkjCompany();
            company.setAddress(address);
            startPage();
            List<XlkjCompany> xlkjCompanies = companyService.selectXlkjCompanyList(company);
            for(XlkjCompany company1:xlkjCompanies){
                RecordVo recordVo = new RecordVo();
                recordVo.setType("3");
                recordVo.setId(company1.getId());
                recordVo.setAddress(company1.getAddress());
                recordVo.setCreateTime(company1.getCreateTime());
                recordList.add(recordVo);
            }
        }
        if(titIndex == 4){
            XlkjPlace place = new XlkjPlace();
            place.setPlaceAddress(address);
            startPage();
            List<XlkjPlace> xlkjPlaces = placeService.selectXlkjPlaceList(place);
            for(XlkjPlace place1:xlkjPlaces){
                RecordVo recordVo = new RecordVo();
                recordVo.setType("4");
                recordVo.setId(place1.getId());
                recordVo.setAddress(place1.getPlaceAddress());
                recordVo.setCreateTime(place1.getCreateTime());
                recordList.add(recordVo);
            }
        }
        if(titIndex == 5){
            XlkjIncident incident = new XlkjIncident();
            incident.setAddress(address);
            startPage();
            List<XlkjIncident> xlkjIncidents = incidentService.selectXlkjIncidentList(incident);
            for(XlkjIncident incident1:xlkjIncidents){
                RecordVo recordVo = new RecordVo();
                recordVo.setType("5");
                recordVo.setId(incident1.getId());
                recordVo.setAddress(incident1.getAddress());
                recordVo.setCreateTime(incident1.getCreateTime());
                recordList.add(recordVo);
            }
        }

        //最终处理
        if(timeIndex  == 1){
            Collections.sort(recordList,new Comparator<RecordVo>(){
                @Override
                public int compare(RecordVo o1, RecordVo o2) {
                    return o2.getCreateTime().toLocaleString().compareTo(o1.getCreateTime().toLocaleString());
                }
            });
        }else if(timeIndex == 2){
            Collections.sort(recordList,new Comparator<RecordVo>(){
                @Override
                public int compare(RecordVo o1, RecordVo o2) {
                    return o1.getCreateTime().toLocaleString().compareTo(o2.getCreateTime().toLocaleString());
                }
            });
        }

        return getDataTable(recordList);
    }


}
